/**
 * created by 小卷毛, 2020/12/18
 * Copyright (c) 2020, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.sgcc.pda.mdrh.task.safe.service;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 描 述：网络状态监听广播
 * 作 者：小卷毛  2020-12-18 15:22
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
public class NetWorkReceiver extends BroadcastReceiver {

    private static final int NETWORK_NONE = -1; //无网络连接
    private static final int NETWORK_WIFI = 0; //wifi
    private static final int NETWORK_MOBILE = 1; //数据网络

    private static long WIFI_TIME = 0;
    private static long ETHERNET_TIME = 0;
    private static long NONE_TIME = 0;
    private static int LAST_TYPE = -3;

    @Override
    public void onReceive(Context context, Intent intent) {
        // 监听网络连接，包括wifi和移动数据的打开和关闭,以及连接上可用的连接都会接到监听
        // 特殊注意：如果if条件生效，那么证明当前是有连接wifi或移动网络的，
        // 如果有业务逻辑最好把esle场景酌情考虑进去！
        if (intent.getAction() != null
                && intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
            long time = getTime();
            if (time != WIFI_TIME
                    && time != ETHERNET_TIME
                    && time != NONE_TIME) {
                int netWorkState = getNetWorkState(context);
                // 这里在判断网络连接状态的同时，
                // 防止收到多条网络连接回调
                if (netWorkState == NETWORK_WIFI
                        && LAST_TYPE != NETWORK_WIFI) {
                    WIFI_TIME = time;
                    LAST_TYPE = netWorkState;
                    Log.d("TAG", "wifi：" + time);
                } else if (netWorkState == NETWORK_MOBILE
                        && LAST_TYPE != NETWORK_MOBILE) {
                    ETHERNET_TIME = time;
                    LAST_TYPE = netWorkState;
                    Log.d("TAG", "数据网络：" + time);
                } else if (netWorkState == NETWORK_NONE
                        && LAST_TYPE != NETWORK_NONE) {
                    NONE_TIME = time;
                    LAST_TYPE = netWorkState;
                    Log.d("TAG", "无网络：" + time);
                }
            }
        }
    }

    //判断网络状态与类型
    public int getNetWorkState(Context context) {
        ConnectivityManager connectivityManager =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetworkInfo != null
                && activeNetworkInfo.isConnected()) {
            switch (activeNetworkInfo.getType()) {
                case ConnectivityManager.TYPE_WIFI:
                    return NETWORK_WIFI;
                case ConnectivityManager.TYPE_MOBILE:
                    return NETWORK_MOBILE;
            }
        } else {
            return NETWORK_NONE;
        }
        return NETWORK_NONE;
    }

    public long getTime() {
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
        String date = sDateFormat.format(new Date());
        return Long.parseLong(date);
    }

}
